package cn.kust.apiuser.controller;

import cn.kust.apiuser.service.VerificationCodeService;
import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import cn.kust.microtourismcommon.pojo.request.VerificationCodeDTO;
import cn.kust.microtourismcommon.pojo.response.NumberCodeResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author WY
 * @data 2022/7/20 15:03
 * 负责登录注册功能，目前做了验证码的功能
 **/
@RestController
@RequestMapping("/api-user")
@Slf4j
public class LoginController {
    @Autowired
    VerificationCodeService verificationCodeService;

    @PostMapping("/verification-code")
    public ResponseResult verificationCode(@RequestBody VerificationCodeDTO verificationCodeDTO){
        String passengerPhone = verificationCodeDTO.getUserPhone();

        log.debug("用户电话号码: ");
        log.debug(passengerPhone);

        Integer code = verificationCodeService.generatorCode(passengerPhone);
        NumberCodeResponse numberCodeResponse = new NumberCodeResponse();
        numberCodeResponse.setNumberCode(code);
        return ResponseResult.success(numberCodeResponse);
    }


    @PostMapping("/verification-code-check")
    public ResponseResult checkVerificationCode(@RequestBody VerificationCodeDTO verificationCodeDTO){
        String userPhone = verificationCodeDTO.getUserPhone();
        String verificationCode = verificationCodeDTO.getVerificationCode();


        log.debug("userPhone:" + userPhone);
        log.debug("verficationCode:" + verificationCode);
        return verificationCodeService.checkCode(userPhone,verificationCode);
    }
}
